﻿#ifndef _RECORD_PROC_H
#define _RECORD_PROC_H

#include "file_manager_arr.h"
#include "rec_rw_factroy.h"

class CRecord1CHProc	//单通录像类
{
public:
    CRecord1CHProc(uint32_t channel, DATA_SOURCE_ID dataID = DATA_SOURCE_RECORD);
    ~CRecord1CHProc();
	int Init(int recordFileType);
    int StartRecord(int type, int64_t time,char *pText = NULL, uint32_t textLen = 0);
	int RecordByRepairFile(int type, int64_t time, char *pText = NULL, uint32_t textLen = 0);
    int StopRecord(int type, bool bCloseFile =false, int newType = RECORD_NONE);
	int InterruptRecord(char * device); //磁盘断线时，及时中断录像
    void Continue();	//继续录像
    void Pause();		//暂停录像
	void Close();		//关闭录像
	void SetDataSource(int dataSourceID);

    //录像接口
    int WriteFrameData(uchar_t * frameBuf, int size);
	int WriteGPSInfo(uchar_t * buf, int size);
	int WritePicFile(uchar_t * frameBuf, int size);

	int CreateGpsInfo(STFS_GPS_IDX & gpsInfo);
    bool IsNeedStop();
	int AnalyzeKeyFrm(const void * buf,uint32_t size);

	void SetVideoParm(int width,int height,int frame_rate);
	void SetAudioParm(int audio_channels, long rate, int bits, int format);

    void SetRecycleRecord(bool bRecycle)
    {
        m_bRecycleRecord = bRecycle;
    }

    bool GetCycleStatus()
    {
        return m_bRecycleRecord;
    }

    int RealRecordType()
    {
        return m_recSegInfo.recordType;
    }

	uint32_t GetCurRecordSN()
	{
		return m_pReoWriter->GetFileHead()->recordSN;
	}

	int GetCacheWater()
	{
		return m_pReoWriter->GetCacheWater();
	}

    inline uint16_t DiskIndex()
    {
        return m_curDiskIdx;
    }

    bool Recording(int recType = RECORD_NONE)
    {
		if(m_pReoWriter->IsOpened())
		{
			return true;
		}

        if(RECORD_NONE == recType)
        {
            return m_bStart;
        }

        if(recType == m_recSegInfo.recordType)
        {
            return m_bStart;
        }
        else
        {
            return false;
        }
    }
	bool isClose()
	{
		return m_pReoWriter->is_closed();
	}
    bool IsPause()
    {
        return m_bSleep;
    }

    int GetCurFilePercent()
    {
        return m_pReoWriter->GetWritePercent();
    }
    int GetFreeSize()
    {
        return m_pReoWriter->GetFreeSize();
    }

    int64_t GetLastFrmTime()
    {
        return m_pReoWriter->GetLastFrmTime();
    }
	
	char * GetRecFile()
	{
		return  m_pReoWriter->GetRecFileName();
	}

    uint32_t GetFileIndex()
    {
        return m_fileIndex;
    }

    uint32_t GetFileSN()
    {
        return m_recSegInfo.recordSN;
    }

    uint32_t GetFileSize()
    {
        return m_pReoWriter->GetLastOffset();
    }

    int GetIdxNum()
    {
        return m_pReoWriter->GetIndexNO();
    }

	void SetRate(int rate)
	{
		m_rate = rate;
	}

	int GetRate()
	{
		return m_rate;
	}

	int64_t GetStartTime()
	{
		return m_startTime;
	}

	bool IsPrepareRec()
	{
		return m_bEnablePre;
	}
	bool IsPrepareData()
	{
		return m_bPrepareData;
	}

	int GetRecTimes()
	{
		return m_pReoWriter->GetRecTimes();
	}

	uint32_t GetWritePercent()
	{
		return m_pReoWriter->GetWritePercent();
	}

	void SetRecDiskIdx(int diskIdx)
	{
		m_curDiskIdx = diskIdx;
	}

	int  GetDataSource()
	{
		return m_dataSourceID;
	}

	const char * GetDevName()
	{
		return m_pReoWriter->GetRecFile()->get_device_name();
///		return m_destDev.c_str();
	}

	void SetResolution(int width, int height)
	{
		m_externInfo.vWidth = width;
		m_externInfo.vHeight= height;
	}

	CRecWriteBase * GetRecWriter()
	{
		return m_pReoWriter;
	}



    bool   GetCheckTime();
	void    CreateFileExternInfo(STFS_RECINFO_EXTERN & externInfo);
	static  int		 PREPARE_SECS;			//预录像时间

protected:
	bool			m_bEnablePre;			//是否开启预录像
	bool			m_bPrepareData;			//是否已经缓冲好预录像
    bool			m_bSleep;				//是否处于休眠状态
    bool			m_bStart;				//是否已经写了开始
    bool			m_bRecycleRecord;		//是否覆盖录像
    uint16_t		m_bOpenFile;			//是否打开文件
    uint16_t		m_diskGroup;			//磁盘分组序号
    uint16_t		m_curDiskIdx;			//磁盘序号
    uint16_t		m_bLockRecord;			//是否锁定录像，锁定状态时，只能手动开启或停止
    uint32_t		m_channel;				//通道编码
	int				m_rate;					//码率

	CFile_manager_arr * m_pRecIndexSet;		//索引管理

    bool			m_bManualRec;			//当前正处于何种录像类型
    uint32_t		m_fileIndex;			//当前文件索引
    uint32_t		m_fileLength;			//记录一次开始到结束写入的数据总长度。

    int64_t			m_startTime;			//录像开始时间
	int64_t			m_stopChkTime;			//最后停止检查时间
	int64_t         m_lastFrmTIme;			//最后一帧时间
    time_t          m_checkTm;
	int		        m_dataSourceID;		    //视频流信息
	CCross_Lock	    m_lock;
	int		        m_recordFileType;		//录像文件类型
	CRecWriteBase*  m_pReoWriter;			//文件写入对象
	REC_FILE_INFO	m_recSegInfo;			//文件信息
	uint32_t        m_recordSN;             //录像文件序列号
	STFS_RECINFO_EXTERN   m_externInfo;

	uint16_t        m_VWidth;				//视频宽
	uint16_t		m_VHeight;              //视频高
	uint16_t        m_vFrameRate;			//视频帧率

	string			m_destFile;				//目标文件
	string          m_destDiskName;         //目标磁盘设备名
	int				m_vFrameCnt;			//帧写入计数

};

#endif	//_RECORD_PROC_H

